package com.cmict.data.infra.dal.mysql.consult;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.infra.controller.admin.consulthistory.vo.ConsultHistoryCreateReqVO;
import com.cmict.data.infra.dal.dataobject.consult.ConsultDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import com.cmict.data.infra.controller.admin.consult.vo.*;

import static com.cmict.data.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 咨询管理 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface ConsultMapper extends BaseMapperX<ConsultDO> {

    default PageResult<ConsultDO> selectPage(ConsultPageReqVO reqVO) {
        QueryWrapper<ConsultDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNoneBlank(reqVO.getIssueTitle()),ConsultDO::getIssueTitle, reqVO.getIssueTitle())
                .eq(StringUtils.isNoneBlank(reqVO.getAnswer()),ConsultDO::getAnswer, reqVO.getAnswer())
                .eq(StringUtils.isNoneBlank(reqVO.getStatus()),ConsultDO::getStatus, reqVO.getStatus())
                .eq(reqVO.getCategoryId() != null,ConsultDO::getCategoryId, reqVO.getCategoryId())
                .orderBy(true ,reqVO.getSortord()!=null && reqVO.getSortord().equals("ASC"),ConsultDO::getCreateTime);
           return selectPage(reqVO,queryWrapper);
    }

    default List<ConsultDO> selectListChoose(String issueTitle,Long id) {
        return selectList(new LambdaQueryWrapperX<ConsultDO>()
                .neIfPresent(ConsultDO::getId,id)
                .like(ConsultDO::getIssueTitle, issueTitle)
                .eq(ConsultDO::getStatus,"1")
                .orderByDesc(ConsultDO::getId));
    }

    default List<ConsultDO> selectList(ConsultExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ConsultDO>()
                .eqIfPresent(ConsultDO::getIssueTitle, reqVO.getIssueTitle())
                .eqIfPresent(ConsultDO::getAnswer, reqVO.getAnswer())
                .eqIfPresent(ConsultDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ConsultDO::getCategoryId, reqVO.getCategoryId())
                .betweenIfPresent(ConsultDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(ConsultDO::getId));
    }

  default   List<ConsultDO> getIssueTitleByRobotChat(ConsultHistoryCreateReqVO createReqVO){
      return  selectList(new LambdaQueryWrapperX<ConsultDO>()
              .eq(ConsultDO::getStatus,"1")
              .likeIfPresent(ConsultDO::getIssueTitle, createReqVO.getChatContent())
              .or()
              .in(ConsultDO::getSimilarIssueTitle,createReqVO.getChatContent())
              .orderByDesc(ConsultDO::getId));
//      return convertList(consultDOS,ConsultDO::getIssueTitle);
  }
    default   List<ConsultDO> getIssueTitleBySimilarRobotChat(ConsultHistoryCreateReqVO createReqVO){
        return  selectList(new LambdaQueryWrapperX<ConsultDO>()
                .eq(ConsultDO::getStatus,"1")
                .likeIfPresent(ConsultDO::getSimilarIssueTitle,createReqVO.getChatContent())
                .orderByDesc(ConsultDO::getId));
    }
}
